package com.tedu.weibotest.controller;


import com.tedu.weibotest.mapper.CommentMapper;
import com.tedu.weibotest.pojo.dto.CommentDTO;
import com.tedu.weibotest.pojo.entity.Comment;
import com.tedu.weibotest.pojo.entity.User;
import com.tedu.weibotest.pojo.vo.CommentListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/comment")
public class CommentController {

    @Autowired
    private CommentMapper commentMapper;

    // 添加评论   状态码 0:失败 1:成功 2:未登录
    @PostMapping("/insert")
    public int insert(@RequestBody CommentDTO commentDTO, HttpSession session){
        User user=(User) session.getAttribute("user");
        if (user!=null){
            Comment comment=new Comment();
            BeanUtils.copyProperties(commentDTO,comment);
            comment.setCreated(new Date());
            comment.setUserId(user.getId());
            int num=commentMapper.insert(comment);
            return num>0?1:0;
        }
        return 2;
    }

    // 查询评论列表
    @GetMapping("/selectByWeiboId")
    public List<CommentListVO> selectByWeiboId(Integer id){
        return commentMapper.selectByWeiboId(id);
    }
}
